package com.jh3502.helper.function.person.personsp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.TextView;

import com.jh3502.helper.R;
import com.jh3502.helper.base.BaseActivity;
import com.jh3502.helper.view.AddAndSubText;
import com.jh3502.helper.view.MyRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/6/1 0001.
 */

public class PersonSpActivity extends BaseActivity {
    @Nullable
    @BindView(R.id.person_sp_recycler)
    MyRecyclerView personSpRecycler;
    @Nullable
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @Nullable
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @Nullable
    @BindView(R.id.person_sp_amouont)
    AddAndSubText personSpAmouont;
    @NonNull
    private List<PersonSpBean> batchUpList = new ArrayList<>();
    private int amount = 0;
    private StringBuffer stringBuffer;

    @Override
    public void responseError(int code, String msg) {

    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            default:
                break;
        }
        return true;
    }

    @Override
    protected int setViewId() {
        return R.layout.activity_person_sp;
    }

    @Override
    protected void initView() {
        toolbar.setTitle("");
        toolbarTitle.setText("快速完成");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        String nameList = getIntent().getStringExtra("nameList");
        String[] batchUp = nameList.split("&");
        for (String s : batchUp) {
            PersonSpBean map = new PersonSpBean(false);
            map.setName(s);
            batchUpList.add(map);
        }

        PersonSpAdapter mAdapter = new PersonSpAdapter(this, batchUpList);
        personSpRecycler.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener((view, pos, isCheck) -> {
            boolean checkTemp = batchUpList.get(pos).getIsCheck();
            batchUpList.get(pos).setIsCheck(!checkTemp);
            stringBuffer = new StringBuffer();
            for (PersonSpBean personSpBean : batchUpList) {
                if (personSpBean.getIsCheck()) {
                    stringBuffer.append(personSpBean.getName() + " ");
                }
            }
            mAdapter.notifyDataSetChanged();
        });
        personSpAmouont.setGoodsStorage(50);
        personSpAmouont.setOnAmountChangeListener((view, n) -> {

            amount = n;
            amount = n;
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {

    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }

    @OnClick(R.id.person_sp_submit)
    public void onPersonSpSubmitClicked() {
        Intent intent = new Intent();
        if (amount == 0) {
            intent.putExtra("addsub", "");
        } else {
            intent.putExtra("addsub", amount + "");
        }
        if (stringBuffer == null) {
            intent.putExtra("content", "");
        } else {
            intent.putExtra("content", stringBuffer.toString());
        }
        setResult(Activity.RESULT_OK, intent);
        onBackPressed();
    }
}
